const puppeteer = require('puppeteer');
const devices = require('puppeteer/DeviceDescriptors');
var request = require('request');
var fs = require('fs');
var request = require('request');
const path = require("path");
const mkdirp =require('mkdirp')
const superagent = require('superagent');
const iPhone = devices['iPhone 6'];
var url = `http://v.douyin.com/dk4pNY/`
var listData = []
puppeteer.launch({'headless': false}).then(async browser => {
  const page = await browser.newPage();
  await page.emulate(iPhone);
  await page.goto(url);
  // other actions...
  console.log(1111)
  // ···························
  let preScrollHeight = 0;
  let scrollHeight = -1;
  while(preScrollHeight !== scrollHeight) {
      page.on('requestfinished', request => {
        console.log('请求')
        // 查看所有请求地址
        // console.log(request.url)
        // ajax
        if (request.resourceType == "xhr") {
          // 匹配所需数据的请求地址
          if(request.url.indexOf('/aweme/v1/aweme/post/?user_id=') != -1) {
            (async () => {
              try {
                // 获取数据并转为json格式
                let res = await request.response();
                // let result = await res.json();
                let result = await res.json();
                let res_data = result.data     
                let list = result.aweme_list.map(item => item.video.play_addr.url_list[0])
                // 接口数据中找到需要的数据 
                listData = listData.concat(list)
                console.log('获取到的数据', list)
                // mkdirp('./video', function () {
                //   console.log('目录：%s 创建成功')
                // })
              } catch(err) {
                console.log(err)
              }
            })()
          }
        }
      });
      console.log('滚动一次')
      // 详情信息是根据滚动异步加载，所以需要让页面滚动到屏幕最下方，通过延迟等待的方式进行多次滚动
      let scrollH1 = await page.evaluate(async () => {
          let h1 = document.body.scrollHeight;
          window.scrollTo(0, h1);
          return h1;
      });
      await page.waitFor(500);
      let scrollH2 = await page.evaluate(async () => {
          return document.body.scrollHeight;
      });
      let scrollResult = [scrollH1, scrollH2];
      preScrollHeight = scrollResult[0];
      scrollHeight = scrollResult[1];
  }
  console.log(2222)
  // ···························
  await browser.close();
  listData.forEach(async (item, index) => {
    // download(item, index, () => {
    //   console.log('下载进度：' + parseInt(index * 100 / list.length) + '%')
    // })
    // dow(item, './video')
    await get(item, index)
  })
});







function formatNum (num) {
  if (num < 10) `00${num}`
  if (num < 100) `0${num}`
  if (num >= 100) num
}
function mkdir(title) {
  console.log('准备创建目录：%s', title);
  if (fs.existsSync(options.dirfile+title)) {
      console.log('目录：%s 已经存在'.error, title);
  }else {
      mkdirp(options.dirfile+title, function (err) {
          console.log('目录：%s 创建成功'.info, title);
      });
  }
}
function download (url, filename) {
  request(url).on('error', function(err) {
     console.log(err)
  // }).pipe(fs.createWriteStream(path+".mp4")).on('finish',()=>{
  }).pipe(fs.createWriteStream(filename+".mp4")).on('finish',()=>{
      console.log('视频下载成功：%s');
  })
}

function get (url, filename) {
  superagent
    .get(url)
    .set({
        'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1'
    })
    .end((err,res) => {
        if(err){
            console.log(err);
            return;
        }
        console.log('页面已加载加载！')
        console.log(res.redirects[0])
        download(res.redirects[0], filename)
    })
}